/**
 * 2013-6-25 下午4:17:58 Created By niexiaoqiang
 */

package com.xiaoqiang.MtwV2ForMoblie.update;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.xiaoqiang.MtwV2ForMoblie.R;
import com.xiaoqiang.util.Logger;

/**
 * 更新确认对话框
 */
public class UpdateConfirmDialog extends Dialog {

	private TextView updatelogview;
	private Button confirmButton;
	private Button cancelButton;

	private static Handler mHandler;
	private DialogResult result;
	private static Logger log = new Logger(UpdateConfirmDialog.class);

	public UpdateConfirmDialog(Context context) {
		super(context, R.style.MyDialog);
		this.setContentView(R.layout.updateconfirm_layout);
		updatelogview = (TextView) this.findViewById(R.id.updateconfirm_content);
		confirmButton = (Button) this.findViewById(R.id.updateconfirm_confirmbuuton);
		confirmButton.setOnClickListener(new UpdateConfirmDialogbuttonlistener());
		cancelButton = (Button) this.findViewById(R.id.updateconfirm_cancelbuuton);
		cancelButton.setOnClickListener(new UpdateConfirmDialogbuttonlistener());
		this.setOnCancelListener(new UpdateConfirmDialogDismissListener(this));
	}

	public void setconfirmButtondisenable() {
		confirmButton.setEnabled(false);
	}

	public DialogResult showDialog(String content) {
		if ("".equals(content) || null == content) {
		} else {
			this.updatelogview.setText(content);
		}
		mHandler = new UpdateConfirmDialogHandler();
		this.show();
		try {
			// 启用消息循环
			Looper.getMainLooper();
			Looper.loop();
		}
		catch (RuntimeException e) {
			log.error(e);
		}
		return result;
	}

	public void endDialog(DialogResult result) {
		this.dismiss();
		this.result = result;
		Message m = mHandler.obtainMessage();
		mHandler.sendMessage(m);
	}

	/**
	 * @function 按钮事件监听器
	 * @author Administrator
	 * 
	 */
	class UpdateConfirmDialogbuttonlistener implements android.view.View.OnClickListener {
		@Override
		public void onClick(View v) {
			if (((Button) v).getId() == R.id.updateconfirm_confirmbuuton) {
				endDialog(DialogResult.CONFIRM);
			} else {
				endDialog(DialogResult.CANCEL);
			}
		}
	}

	/**
	 * @author niexiaoqiang
	 * @function 防止用户点击屏幕其他地方导致没有返回值的问题
	 */
	class UpdateConfirmDialogDismissListener implements DialogInterface.OnCancelListener {
		UpdateConfirmDialog updateConfirmDialog;

		public UpdateConfirmDialogDismissListener(UpdateConfirmDialog updateConfirmDialog) {
			this.updateConfirmDialog = updateConfirmDialog;
		}

		@Override
		public void onCancel(DialogInterface dialog) {
			updateConfirmDialog.endDialog(DialogResult.CANCEL);
		}
	}

	static class UpdateConfirmDialogHandler extends Handler {
		@Override
		public void handleMessage(Message mesg) {
			throw new RuntimeException("message looper throws,don't catch");
		}
	}

	public enum DialogResult {
		CONFIRM, CANCEL
	}
}
